<?
//Light menu 0.0.11
class sym_light_menu extends symbiont{
    public function ___main($design='', $menu=0, $parent=0, $levels=0, $active=1, $hidden=0, $class=''){
        $des=$design?$design:'main';
        $design=$this->_check($design, 'main');
        global $sql, $kernel, $sym;
        $this->content='';
        $this->menu_class=$class;
        $this->item_class='';
        $this->a_class='';
        if(!is_int($parent)){
            $parent=$sql->select_each('pages', 'id', array('name'=>$parent), '', '', 1);
        }
        if($active){
            $actives=array($kernel->page->id);
            $x=$kernel->page->id;
            while($x){
                $x=$sql->select_each('pages', 'parent_id', array('id'=>$x), '', '', 1);
                if($x=='0') break;
                $actives[]=$x;
            }
        }
        else{
            $actives='';
        }
        if($menu){
            if(!is_int($menu)){
                $menu=$sql->select_each('sym_light_menu', 'id', array('name'=>$menu), '', '', 1);
            }
            $kernel->library_add('cache');
            $cname='light_menu/handmade/'.$menu.'/'.$kernel->page->id.$des.'.'.$kernel->lang->id;
            $c=new cache($cname);
            if($c->cache){
                $d=new design($design, 'light_menu', false);
                $this->get_handmade($menu);
                $d->inc();
            }
            $c->finish();
        }
        else{
            $d=new design($design, 'light_menu', false);
            $this->content=$this->get_level($parent, $levels, $actives, $active, $hidden);
            $d->inc();
        }
    }
    //Menus list
    public function ___admin($design=''){
        global $kernel, $sym;
        $kernel->symbiont_add('light_menu-admin');
        $sym->light_menu_admin->main('');
    }
    
    public function get_level($parent=0, $levels=0, $actives='', $active=0, $hidden=1){
        global $sql, $kernel;
        $ret='';
        $where=array('parent_id'=>$parent);
        if($active!=='?') $where['is_active']=$active;
        if($hidden!=='?') $where['is_hidden']=$hidden;
        $pages=$sql->select_each('pages', array('id', 'title_lid', 'name', 'is_home'), $where, '', 'id');
        if(is_array($pages)){
            $items='';
            foreach($pages as $val){
                $item_class='';
                if(is_array($actives)){
                    $t=false;
                    foreach($actives as $id){
                        if($id==$val['id']){
                            $item_class="opened";
                            if($id==$kernel->page->id) $item_class.=' current';
                            $t=true; break;
                        }
                    }
                }
                else{
                    $t=false;
                }
                if($levels>1){
                    $this->sub=$this->get_level($val['id'], $levels-1, $actives, $active, $hidden);
                }
                elseif($levels==0){
                    $this->sub=$this->get_level($val['id'], 0, $actives, $active, $hidden);
                }
                else{
                    $this->sub=$t?$this->get_level($val['id'], -1, $actives, $active, $hidden):'';
                }
                $l=new label($val['title_lid']);
                $this->id=$val['id'];
                $this->name=$val['name'];
                $this->title=$l->get();
                if(!$this->title) $this->title=$val['name'];
                $this->url=$val['name'].'/';
                $this->item_class=$item_class;
                if($sql->count('positions', array('name'=>'page_'.$val['name']), 1)==0){
                    $this->a_class='empty';
                }
                else{
                    $this->a_class='';
                }
                if($this->title) $items.=design::run($this->item);
            }
            $this->items=$items;
            $ret.=design::run($this->menu);
        }
        return $ret;
    }
    public function get_handmade($menu, $parent=0, $level=0){
        global $sql, $kernel;        
        $pages=$sql->query_each("SELECT l.value as title, p.id, p.is_home, p.name, l2.value as title2, h.link,
                                    exists(
                                        SELECT 1 FROM `sym_light_menu_handmade` WHERE parent_id=h.id LIMIT 1
                                    ) as sub
                                FROM `sym_light_menu_handmade` as h
                                    LEFT JOIN `pages` as p ON h.page_id=p.id
                                    LEFT JOIN `labels` AS l ON l.id=p.title_lid AND l.lng_id=".$kernel->lang->id."
                                    LEFT JOIN `labels` AS l2 ON l2.id=h.title_lid AND l2.lng_id=".$kernel->lang->id."
                                WHERE h.menu_id=".$menu." AND h.parent_id=".$parent."
                                ORDER BY position");
        if(is_array($pages)){
            $items='';
            foreach($pages as $page){
                if($page['sub']){
                    $sub=$this->get_handmade($menu, $page['id'], $level+1);
                    $this->sub=$sub;
                }
                else{
                    $this->sub='';
                }
                $this->id=$page['id'];
                if($page['title2']){
                    $this->title=$page['title2'];
                }
                else{
                    $this->title=$page['title'];
                }
                if($page['link']){
                    $this->url=$page['link'];
                }
                else{
                    $this->url=$kernel->site->url.($kernel->site->mlm=='true'?$kernel->lang->abbr.'/':'').($page['is_home']==0?$page['name'].'/':'');
                }
                $this->item_class=$kernel->page->id==$page['id']?'active':'';
                if($this->title) $items.=design::run($this->item);
            }
            $this->items=$items;
            $this->content=design::run($this->menu);
            return $this->content;
        }
        return '';
    }
    public function _administrate($symbiont){
        global $sql;
        $design=$this->_check('administrate');
        $d=new design($design, 'light_menu', false);
        $this->options='';
        $menus=$sql->query_each("
                            SELECT m.id, m.name
                            FROM `sym_light_menu` as m
                            ORDER BY name");
        foreach($menus as $menu){
            $this->id=$menu['id'];
            $this->name=$menu['name'];
            $this->options.=design::run($this->option);
        }
        $d->inc();
    }
}
?>